package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.reggie.configuration.CustomException;
import com.reggie.dto.DishDto;
import com.reggie.mapper.DishMapper;
import com.reggie.pojo.Dish;
import com.reggie.pojo.DishFlavor;
import com.reggie.service.DishFlavorService;
import com.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品基本信息，同时新增口味信息
     * @param dishDto
     */
    @Override
    @Transactional //进行两张表的操作，需要开启事务
    public void saveWithFlavors(DishDto dishDto) {
        //保存菜品的基本信息
        this.save(dishDto);

        //获取菜品id
        Long dishId = dishDto.getId();

        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存口味的信息
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据id查询菜品信息和口味信息
     * @param id
     * @return
     */
    public DishDto getByIdWithFlavors(Long id){
        //查询当前菜品基本信息，从dish表中查
        Dish dish = this.getById(id);

        DishDto dishDto = new DishDto();
        //拷贝商品信息
        BeanUtils.copyProperties(dish,dishDto);

        //查询当前菜品对应的口味信息，从dish_flavors表中查
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());

        //执行方法查询
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        //设置口味信息
        dishDto.setFlavors(flavors);

        return dishDto;

    }


    @Transactional  //添加事务，保证数据一致性
    @Override
    public void updateWithFlavors(DishDto dishDto) {
        //根据id修改菜品信息
        this.updateById(dishDto);

        //清理当前菜品对应的口味数据，相当于dish_flavors的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //新增提交的菜品对应的口味数据，相当于dish_flavors的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            //设置口味的菜品id
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 删除菜品以及关系表中的数据
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithFlavors(List<Long> ids) {
        //查询菜品状态
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,ids);
        wrapper.eq(Dish::getStatus,1);
        long count = this.count(wrapper);

        if(count > 0){
            //不可删除，抛出异常
            throw new CustomException("菜品正在售卖中，不可删除");
        }

        //删除dish表的数据
        this.removeByIds(ids);

        //删除菜品的对应口味
        // delete from dish_flavor where dish_id in ()
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(queryWrapper);
    }
}
